package com.lagou.wang.servlet.classes;

import com.lagou.wang.pojo.Class;
import com.lagou.wang.pojo.Student;
import com.lagou.wang.service.classes.ClassService;
import com.lagou.wang.service.classes.ClassServiceImpl;
import com.lagou.wang.service.student.StudentService;
import com.lagou.wang.service.student.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.List;

@WebServlet(name = "ClassServlet", urlPatterns = "/jsp/ClassServlet")
public class ClassServlet extends HttpServlet {
    ClassService classService = new ClassServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param = request.getParameter("param");

        if (param.equals("all")) {
            this.queryAllClass(request, response, "allClass.jsp");
        } else if (param.equals("add")) {
            this.addClass(request, response);
            this.queryAllClass(request, response, "allClass.jsp");
        } else if (param.equals("toUpdate")) {
            response.sendRedirect("updateClass.jsp");
            int cid = Integer.parseInt(request.getParameter("cid"));
            Class clazz = this.queryClassById(request, response, cid);
            request.getSession().setAttribute("clazz", clazz);
        } else if (param.equals("update")) {
            this.updateStudentById(request, response);
            this.queryAllClass(request, response, "allClass.jsp");
        } else if (param.equals("del")) {
            this.deleteClassById(request, response);
            this.queryAllClass(request, response, "allClass.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void queryAllClass(HttpServletRequest request, HttpServletResponse response, String url) throws IOException {
        List<Class> classList = classService.queryAllClass();
        request.getSession().setAttribute("classList", classList);
        response.sendRedirect(url);

    }

    public void addClass(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int cid = 0;
        String className = request.getParameter("className");
        String grade = request.getParameter("grade");
        String teacher = request.getParameter("teacher");
        String slogan = request.getParameter("slogan");
        int size = Integer.parseInt(request.getParameter("size"));
        classService.addClass(new Class(cid, className, grade, teacher, slogan, size));


    }

    public void deleteClassById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StudentService studentService = new StudentServiceImpl();
        int cid = Integer.parseInt(request.getParameter("cid"));


        Student students = studentService.queryStudentByClassId(cid);
        if (students == null) {
            classService.deleteClassById(cid);
        }else {
            request.getSession().setAttribute("mess","该班级内有学生关联，无法删除！");
            response.getWriter().print("<script>alert('你好！bai');</script>");
            System.out.println("该班级内有学生关联，无法删除！");

        }


    }

    public void updateStudentById(HttpServletRequest request, HttpServletResponse response) {
        int cid = Integer.parseInt(request.getParameter("cid"));
        String className = request.getParameter("className");
        String grade = request.getParameter("grade");
        String teacher = request.getParameter("teacher");
        String slogan = request.getParameter("slogan");
        int size = Integer.parseInt(request.getParameter("size"));
        classService.updateClassById(new Class(cid, className, grade, teacher, slogan, size));

    }


    public Class queryClassById(HttpServletRequest request, HttpServletResponse response, int id) {
        Class aClass = classService.queryClassById(id);
        return aClass;
    }

}
